package com.cogent.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;

import com.cogent.common.cache.BaseCache;
import com.cogent.common.cache.CacheFactory;
import com.cogent.common.util.StringUtil;
import com.cogent.dao.domain.QueryParamDomain;
import com.cogent.dao.facade.BussinessDaoFacade;

public class BussinessQueryHelper {
	//日志对象
	private static Logger _log = Logger.getLogger(BussinessQueryHelper.class); 
	private static int _log_mins = 2000;
	private static BaseCache basecache = CacheFactory.getInstance();
	
	public static List query(String bussisscode, String sql) {
		return queryWithSql(bussisscode, sql, null);
	}
	
	public static List query(String sql) {
		return queryWithSql(getTableName(sql), sql, null);
	}
	
	public static List query(String sql, BussinessDaoFacade bussinessFacade) {
		String tableName = getTableName(sql);
		return query(tableName, sql, bussinessFacade);
	}
	
	public static List query(String bussisscode, String sql, BussinessDaoFacade bussinessFacade) {
		return queryWithSql(bussisscode, sql, bussinessFacade);
	}
	
	private static List queryWithSql(String bussisscode, String sql, BussinessDaoFacade bussinessFacade) {
		if(StringUtil.isEmpty(sql))
			return new ArrayList();
		String cachekey = CacheFactory.getCahcheKey(sql);
		List ret = basecache.getList(bussisscode, cachekey);
		if(ret == null){
			if(bussinessFacade == null) {
				BussinessInstance bussinessInstance = new BussinessInstance();
				bussinessFacade = bussinessInstance.getBussinessFacade();
			}
			ret = bussinessFacade.query(sql);
			basecache.putList(bussisscode, cachekey, ret);
		}
		
		return ret;
	}
	
	public static void refQueryCache(String sql) {
		refQueryCache(getTableName(sql), sql, null);
	}
	
	public static void refQueryCache(String bussisscode, String sql) {
		refQueryCache(bussisscode, sql, null);
	}
	
	public static void refQueryCache(String sql, BussinessDaoFacade bussinessFacade) {
		refQueryCache(getTableName(sql), sql, bussinessFacade);
	}
	
	public static void refQueryCache(String bussisscode, String sql, BussinessDaoFacade bussinessFacade) {
		if(bussinessFacade == null) {
			BussinessInstance bussinessInstance = new BussinessInstance();
			bussinessFacade = bussinessInstance.getBussinessFacade();
		}
		String cachekey = CacheFactory.getCahcheKey(sql);
		List ret = bussinessFacade.query(sql);
		basecache.putList(bussisscode, cachekey, ret);
	}
	
	public static List query(String bussisscode, String sql, List<QueryParamDomain> valList) {
		return query(bussisscode, sql, valList, "", null);
	}
	
	public static List query(String sql, List<QueryParamDomain> valList) {
		return query(getTableName(sql), sql, valList, "", null);
	}
	
	public static List query(String bussisscode, String sql, List<QueryParamDomain> valList, String groupBy) {
		return query(bussisscode, sql, valList, groupBy, null);
	}
	
	public static List query(String sql, List<QueryParamDomain> valList, String groupBy) {
		return query(getTableName(sql), sql, valList, groupBy, null);
	}
	
	public static List query(String bussisscode, String sql, List<QueryParamDomain> queryList, String groupBy, BussinessDaoFacade bussinessFacade) {
		if(StringUtil.isEmpty(sql))
			return new ArrayList();
		String curkey = StringUtil.getLinkString(StringUtil.getLinkString(sql, queryList.toString()), groupBy);
		String cachekey = CacheFactory.getCahcheKey(curkey);
		List ret = basecache.getList(bussisscode, cachekey);
		if(ret == null) {
			if(bussinessFacade == null) {
				BussinessInstance bussinessInstance = new BussinessInstance();
				bussinessFacade = bussinessInstance.getBussinessFacade();
			}
			ret = bussinessFacade.query(sql, queryList, groupBy);
			basecache.putList(bussisscode, cachekey, ret);
		}
		
		return ret;
	}
	
	public static HashMap queryOneRow(String bussisscode, String sql) {
		return queryOneRowWithSql(bussisscode, sql, null);
	}
	
	public static HashMap queryOneRow(String sql) {
		return queryOneRowWithSql(getTableName(sql), sql, null);
	}
	
	public static HashMap queryOneRow(String sql, BussinessDaoFacade bussinessFacade) {
		String tableName = getTableName(sql);
		return queryOneRow(tableName, sql, bussinessFacade);
	}
	
	public static HashMap queryOneRow(String bussisscode, String sql, BussinessDaoFacade bussinessFacade) {
		return queryOneRowWithSql(bussisscode, sql, bussinessFacade);
	}
	
	private static HashMap queryOneRowWithSql(String bussisscode, String sql, BussinessDaoFacade bussinessFacade) {
		if(StringUtil.isEmpty(sql))
			return new HashMap();
		String cachekey = CacheFactory.getCahcheKey(sql);
		HashMap ret = basecache.getMap(bussisscode, cachekey);
		if(ret == null) {
			if(bussinessFacade == null) {
				BussinessInstance bussinessInstance = new BussinessInstance();
				bussinessFacade = bussinessInstance.getBussinessFacade();
			}
			ret = bussinessFacade.getOneRow(sql);
			basecache.putMap(bussisscode, cachekey, ret);
		}
		
		return ret;
	}
	
	public static HashMap queryOneRow(String sql, List<QueryParamDomain> valList) {
		return queryOneRow(getTableName(sql), sql, valList, null);
	}
		
	public static HashMap queryOneRow(String bussisscode, String sql, List<QueryParamDomain> valList) {
		return queryOneRow(bussisscode, sql, valList, null);
	}

	public static HashMap queryOneRow(String bussisscode, String sql, List<QueryParamDomain> queryList, BussinessDaoFacade bussinessFacade) {
		if(StringUtil.isEmpty(sql))
			return new HashMap();
		String curkey = StringUtil.getLinkString(sql, queryList.toString());
		String cachekey = CacheFactory.getCahcheKey(curkey);
		HashMap ret = basecache.getMap(bussisscode, cachekey);
		if(ret == null) {
			if(bussinessFacade == null) {
				BussinessInstance bussinessInstance = new BussinessInstance();
				bussinessFacade = bussinessInstance.getBussinessFacade();
			}
			ret = bussinessFacade.getOneRow(sql, queryList);
			basecache.putMap(bussisscode, cachekey, ret);
		}
		
		return ret;
	}
	
	public static void refOneRowCache(String sql, BussinessDaoFacade bussinessFacade) {
		refOneRowCache(getTableName(sql), sql, bussinessFacade);
	}
	
	public static void refOneRowCache(String bussisscode, String sql, BussinessDaoFacade bussinessFacade) {
		if(bussinessFacade == null) {
			BussinessInstance bussinessInstance = new BussinessInstance();
			bussinessFacade = bussinessInstance.getBussinessFacade();
		}
		String cachekey = CacheFactory.getCahcheKey(sql);
		HashMap ret = bussinessFacade.getOneRow(sql);
		basecache.putMap(bussisscode, cachekey, ret);
	}
	
	public static void refOneRowCache(String bussisscode, String sql, HashMap ret) {
		String cachekey = CacheFactory.getCahcheKey(sql);
		basecache.putMap(bussisscode, cachekey, ret);
	}
	
	public static HashMap getValueById(String tableName, String id) {
		return getValueById(tableName, id, null);
	}
	
	public static HashMap getValueById(String tableName, String id, BussinessDaoFacade bussinessFacade) {
		if(StringUtil.isEmpty(id))
			return new HashMap();
		HashMap ret = basecache.getMap(tableName, id);
		if(ret == null) {
			if(bussinessFacade == null) {
				BussinessInstance bussinessInstance = new BussinessInstance();
				bussinessFacade = bussinessInstance.getBussinessFacade();
			}
			List queryList = new ArrayList();
			queryList.add(new QueryParamDomain("id", id));
			ret = bussinessFacade.getByID(tableName, queryList);
			basecache.putMap(tableName, id, ret);
		}
		
		return ret;
	}
	
	public static void refByIdCache(String tableName, String id) {
		refByIdCache(tableName, id, null);
	}
	
	public static void refByIdCache(String tableName, String id, BussinessDaoFacade bussinessFacade) {
		if(bussinessFacade == null) {
			BussinessInstance bussinessInstance = new BussinessInstance();
			bussinessFacade = bussinessInstance.getBussinessFacade();
		}
		List queryList = new ArrayList();
		queryList.add(new QueryParamDomain("id", id));
		HashMap ret = bussinessFacade.getByID(tableName, queryList);
		basecache.putMap(tableName, id, ret);
	}
	
	public static List queryByTableName(String tableName, List<QueryParamDomain> queryList) {
		return queryByTableName(tableName, queryList, null);
	}
	
	public static List queryByTableName(String tableName, List<QueryParamDomain> queryList, BussinessDaoFacade bussinessFacade) {
		return queryByTableName(tableName, queryList, "", bussinessFacade);
	}
	
	public static List queryByTableName(String tableName, List<QueryParamDomain> queryList, String postfix, BussinessDaoFacade bussinessFacade) {
		if(queryList.size() == 0)
			return new ArrayList();
		String curkey = StringUtil.getLinkString(queryList.toString(), postfix);
		String cachekey = CacheFactory.getCahcheKey(curkey);
		List ret = basecache.getList(tableName, cachekey);
		if(ret == null) {
			if(bussinessFacade == null) {
				BussinessInstance bussinessInstance = new BussinessInstance();
				bussinessFacade = bussinessInstance.getBussinessFacade();
			}
			ret = bussinessFacade.queryWithPostfix(tableName, queryList, postfix);
			basecache.putList(tableName, cachekey, ret);
		}
		
		return ret;
	}
	
	public static List queryNotCache(String sql) {
		return queryNotCache(sql, null);
	}
	
	public static List queryNotCache(String sql, BussinessDaoFacade bussinessFacade) {
		if(bussinessFacade == null) {
			BussinessInstance bussinessInstance = new BussinessInstance();
			bussinessFacade = bussinessInstance.getBussinessFacade();
		}
		List ret = bussinessFacade.query(sql);
		return ret;
	}
	
	public static HashMap queryOneRowNotCahce(String sql) {
		return queryOneRowNotCahce(sql, null);
	}
	
	public static HashMap queryOneRowNotCahce(String sql, BussinessDaoFacade bussinessFacade) {
		if(bussinessFacade == null) {
			BussinessInstance bussinessInstance = new BussinessInstance();
			bussinessFacade = bussinessInstance.getBussinessFacade();
		}
		HashMap ret = bussinessFacade.getOneRow(sql);
		
		return ret;
	}
	
	public static HashMap getValueByIdNotCahce(String tableName, List<QueryParamDomain> queryList) {
		return getValueByIdNotCahce(tableName, queryList, null);
	}
	
	public static HashMap getValueByIdNotCahce(String tableName, List<QueryParamDomain> queryList, BussinessDaoFacade bussinessFacade) {
		if(bussinessFacade == null) {
			BussinessInstance bussinessInstance = new BussinessInstance();
			bussinessFacade = bussinessInstance.getBussinessFacade();
		}
		HashMap ret = bussinessFacade.getByID(tableName, queryList);
		
		return ret;
	}
	
	public static List queryNotCahce(String sql, List<QueryParamDomain> valList, String groupBy) {
		return queryNotCahce(getTableName(sql), sql, valList, groupBy, null);
	}
	
	public static List queryNotCahce(String bussisscode, String sql, List<QueryParamDomain> queryList, String groupBy, BussinessDaoFacade bussinessFacade) {
		if(StringUtil.isEmpty(sql))
			return new ArrayList();
		if(bussinessFacade == null) {
			BussinessInstance bussinessInstance = new BussinessInstance();
			bussinessFacade = bussinessInstance.getBussinessFacade();
		}
		List ret = bussinessFacade.query(sql, queryList, groupBy);
		
		return ret;
	}

	public static String get(String key) {
		return basecache.getString(key);		
	}
	
	public static void put(String key, String value) {
		basecache.putString(key, value);
	}
	
	public static void remove(String busid, String id) {	
		basecache.remove(busid, id);
	}

	public static void removePrefix(String busid, String prefix) {
		basecache.removePrefix(busid, prefix);
	}
	
	public static void removeByKey(String key) {
		basecache.removeByKey(key);
	}
	
	public static void removeBySql(String busid, String sql) {	
		String cachekey = CacheFactory.getCahcheKey(sql);
		basecache.remove(busid, cachekey);
	}
	
	public static String getTableName(String sql) {
		String temp = sql.toLowerCase();
		temp = temp.replaceAll("\n", " ");
		temp = temp.replaceAll("\r", " ");
		temp = temp.replaceAll("\t", " ");
		while(temp.indexOf("from (") > 0)
			temp = temp.substring(temp.indexOf("from (") + 6);
		if(temp.indexOf(" from ")>0) {
			temp = temp.substring(temp.indexOf(" from ") + 6);
			int start = temp.indexOf(" ");
			if(start == 0)
				temp = temp.substring(1);
			start = temp.indexOf(" ");
			if(start > 0)
				temp = temp.substring(0, start);
		}
		
		return temp;
	}	
	
	public static BaseCache getBaseCache() {
		return basecache;
	}
}
